﻿$.ui.ready(function() {
	function showPersons(personProfile) {
		var listItem = $.create('<li class="auto-added-list"><a style="border-color:red;" class="list-anchor-link" href="#healthDataList"><img style="width:50px; height:55px;" src="images/male.png" /></a></li>');
		listItem.html(listItem.html() + personProfile.name);
		$("#personsImageList").append(listItem);
		$("#personsImageList li").bind("click", function() {
			var index = $(this).index();
			$N.ctrl.PersonProfileCtrl.setSelectedData(index);
		});
	}

	$N.ctrl.UserToPersonCtrl.registerClientNotify($N.ctrl.PersonProfileCtrl.getPersonProfileData);
	$N.ctrl.PersonProfileCtrl.registerClientNotify(showPersons);
	$N.ctrl.UserToPersonCtrl.getUserToPersonData();
});

function restoreToNormal() {
	$('.auto-added-list-checkbox').remove();
	$('#auto-added-header-checkbox').remove();
	$('#auto-added-cancel-btn').remove();
	$('.list-anchor-link').show();
	$('a').removeClass("pressed");
	$N.util.setHomePageMode($N.util.idle);

}

function deleteUserCallback() {
	if ($N.util.getHomePageMode() === $N.util.idle) {
		//show everything.
		$N.util.setHomePageMode($N.util.deletingUserMode);
		//hide all <a>
		$('.list-anchor-link').hide();
		//append a checkbox on every list item
		var listItems = $('.auto-added-list');
		listItems.each(function() {
			var listCheckItem = $.create("<input type='checkbox' class='auto-added-list-checkbox' style='float:right; right: 10px; margin-top: 20px;display:block;'/>");
			$(this).append(listCheckItem);
		});

		$('.auto-added-list-checkbox').bind('click', function() {
			if ($(this).prop('checked') === true) {
				$(this).prop('checked', false);
			} else {
				$(this).prop('checked', true);
			}
		});
		//add one checkbox at header
		var checkItem = $.create('input', {
			type : 'checkbox',
			id : 'auto-added-header-checkbox'
		});
		checkItem.css('float', 'right');
		checkItem.css('right', '15px');
		checkItem.css('margin-top', '20px');
		checkItem.css('display', 'inline-block');

		checkItem.bind('click', function() {
			if (!$(this).prop('checked')) {
				$(this).prop('checked', true);
				$('.auto-added-list-checkbox').each(function() {
					$(this).prop('checked', true);
				});
			} else {
				$(this).prop('checked', false);
				$('.auto-added-list-checkbox').each(function() {
					$(this).prop('checked', false);
				});
			}
		});

		var cancelBtnItem = $.create('a', {
			id : 'auto-added-cancel-btn'
		});
		cancelBtnItem.css('float', 'right');
		cancelBtnItem.css('right', '10px');

		cancelBtnItem.addClass('button orange');
		cancelBtnItem.html("取消");
		$('#header').append(checkItem);
		$('#header').append(cancelBtnItem);

		cancelBtnItem.bind('click', function() {
			restoreToNormal();
			$N.util.setHomePageMode($N.util.idle);
			return;
		});
	} else if($N.util.getHomePageMode() === $N.util.deletingUserMode) {
		//we will detach the users
		var listItems = $('.auto-added-list');
		var deletePersonsArray = [];
		var personProfiles = $N.ctrl.PersonProfileCtrl.getPersonProfiles();
		listItems.each(function() {
			var checkbox = $(this).children($('.auto-added-list-checkbox'));
			if (checkbox.prop('checked')) {
				var index = $(this).index();
				var personProfile = personProfiles[index];
				var personID = personProfile.personID;
				deletePersonsArray.push(personID);
			}
		});
		$N.ctrl.UserToPersonCtrl.detachManagedPersons(deletePersonsArray);
		$N.util.setHomePageMode($N.util.idle);
		restoreToNormal();
		return;
	} else{
		$N.util.setHomePageMode($N.util.idle);
		return;
	}
}

function addUserCallback() {

	//check if it is in delete mode
	if ($N.util.getHomePageMode() === $N.util.addUserMode) {
		restoreToNormal();
		return;
	}

	$N.util.setHomePageMode($N.util.addUserMode);
	//hide all <a>
	$('.list-anchor-link').hide();
	//append a checkbox on every list item
	var listItems = $('.auto-added-list');
	listItems.each(function() {
		var listCheckItem = $.create("<input type='checkbox' class='auto-added-list-checkbox' style='float:right; right: 10px; margin-top: 20px;display:block;'/>");
		$(this).append(listCheckItem);
	});

	$('.auto-added-list-checkbox').bind('click', function() {
		if ($(this).prop('checked') === true) {
			$(this).prop('checked', false);
		} else {
			$(this).prop('checked', true);
		}
	});
	//add one checkbox at header
	var checkItem = $.create('input', {
		type : 'checkbox',
		id : 'auto-added-header-checkbox'
	});
	checkItem.css('float', 'right');
	checkItem.css('right', '15px');
	checkItem.css('margin-top', '20px');
	checkItem.css('display', 'inline-block');

	checkItem.bind('click', function() {
		if (!$(this).prop('checked')) {
			$(this).prop('checked', true);
			$('.auto-added-list-checkbox').each(function() {
				$(this).prop('checked', true);
			});
		} else {
			$(this).prop('checked', false);
			$('.auto-added-list-checkbox').each(function() {
				$(this).prop('checked', false);
			});
		}
	});

	var addAllBtnItem = $.create('a', {
		id : 'auto-added-add-all-btn'
	});
	addAllBtnItem.css('float', 'right');
	addAllBtnItem.css('right', '10px');

	addAllBtnItem.addClass('button orange');
	addAllBtnItem.html("添加");
	$('#header').append(checkItem);
	$('#header').append(addAllBtnItem);

	addAllBtnItem.bind('click', function() {
		var listItems = $('.auto-added-list'), deletePersonsArray = [];
		listItems.each(function() {
			var checkbox = $(this).children($('.auto-added-list-checkbox').get(0));
			if (checkbox.prop('checked')) {
				var personID = $(this).data('personID');
				deletePersonsArray.push(personID);
			}
		});
		$N.UserToPersonCtrl.detachManagedPersons(deletePersonsArray);
	});
}

